package cn.wolfcode.luowowo.mgrsite.controller;

import cn.wolfcode.luowowo.article.domain.StrategyContent;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.service.IStrategyCatalogService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.IStrategyTagService;
import cn.wolfcode.luowowo.article.service.IStrategyThemeService;
import cn.wolfcode.luowowo.common.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/strategyDetail")
public class StrategyDetailController {
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IStrategyCatalogService strategyCatalogService;
    @Reference
    private IStrategyThemeService strategyThemeService;
    @Reference
    private IStrategyTagService strategyTagService;

    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") StrategyDetailQuery qo, Model model) {
        model.addAttribute("pageInfo",strategyDetailService.query(qo));
        return "strategyDetail/list";
    }

    @RequestMapping("/input")
    public String input(Long id, Model model) {

        if (id != null) {
            StrategyDetail strategyDetail = strategyDetailService.get(id);//文章
            StrategyContent content = strategyDetailService.getContent(id);//内容
            strategyDetail.setStrategyContent(content);
            //回显文章及内容
            model.addAttribute("entity", strategyDetail);
            //回显标签
            model.addAttribute("tags", strategyTagService.getByDetailId(id));
        }
        model.addAttribute("catalogs", strategyCatalogService.list());//分类下拉框
        model.addAttribute("themes", strategyThemeService.list());//主题下拉框
        return "strategyDetail/input";
    }

    @ResponseBody
    @RequestMapping("/saveOrUpdate")
    public Object saveOrUpdate(StrategyDetail entity, String tags) {
        AjaxResult result = new AjaxResult();
        try {
            strategyDetailService.saveOrUpdate(entity, tags);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("操作失败");
        }
        return result;
    }

    @ResponseBody
    @RequestMapping("/changeState")
    public Object changeState(Long id, Integer state) {
        AjaxResult result = new AjaxResult();
        try {
            strategyDetailService.changeState(id, state);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("操作失败");
        }
        return result;
    }
}
